package com.vogue.family.ui.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.vogue.family.data.api.NetWorkManager;
import com.vogue.family.data.entity.BaseResponse;
import com.vogue.family.data.entity.User;
import java.util.HashMap;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

public class SignUpViewModel extends AndroidViewModel {

    private MutableLiveData<String> errorMsg;

    public SignUpViewModel(@NonNull Application application) {
        super(application);
        errorMsg=new MutableLiveData<>();
    }
    public MutableLiveData<String> getErrorMsg() {
        return errorMsg;
    }
    public void onSignUp(String nickName,String account,String email,String password){

        HashMap<String,String> map=new HashMap<>();

        map.put("nickName",nickName);
        map.put("account",account);
        map.put("email",email);
        map.put("password",password);

        NetWorkManager
                .getInstance()
                .getApiService()
                .signUp(map)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {

                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseResponse<User>>() {

                    @Override
                    public void onNext(BaseResponse<User> response) {
                        if (response.getCode()==0){

                            User data = response.getData();

                        }else {
                            errorMsg.setValue(""+response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        errorMsg.setValue("错误信息："+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
